
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BGridLayout>

#include <BLabel>
#include <BButton>

#include "Settings.h"
#include "ExtentionEditor.h"

ExtentionEditor::ExtentionEditor(BWidget* parent) : BDialog(parent)
{
	this->setTitle("Extention Setting");

	BHBoxLayout* topLayout = new BHBoxLayout(this, Part_Bottom);
	{
		_insertButton = new BButton("Insert");
		_removeButton = new BButton("Remove");
		topLayout->addSpring();
		topLayout->addWidget(_insertButton);
		topLayout->addWidget(_removeButton);
	}

	_tableWidget = new BTableWidget();
	_tableWidget->setColumnCount(2);
	_tableWidget->setColumnLabel(0, "Extention");
	_tableWidget->setColumnLabel(1, "Style");
	{
		_lineEdit = new BLineEdit(_tableWidget);
		_lineEdit->hide();

		_styleComboBox = new BComboBox(_tableWidget);
		_styleComboBox->addItem("xml");
		_styleComboBox->addItem("yaml");
		_styleComboBox->addItem("son");
		_styleComboBox->addItem("css");
		_styleComboBox->addItem("bcs");
		_styleComboBox->hide();

	}


	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(topLayout, 0);
	vlayout->addWidget(_tableWidget);

	BHBoxLayout* bottomLayout = new BHBoxLayout(this, Part_Bottom);
	{
		_confirmButton = new BButton("Confirm");
		_applyButton = new BButton("Apply");
		_cancelButton = new BButton("Cancel");
		bottomLayout->addWidget(_confirmButton);
		bottomLayout->addSpring();
		bottomLayout->addWidget(_applyButton);
		bottomLayout->addWidget(_cancelButton);
	}

	connect(this, Signal_AssetChanged, &ExtentionEditor::slotAssetChanged);

	connect(_insertButton, Signal_Clicked, &ExtentionEditor::slotInsertButtonClicked);
	connect(_removeButton, Signal_Clicked, &ExtentionEditor::slotRemoveButtonClicked);
	connect(_tableWidget, Signal_ItemClicked, &ExtentionEditor::slotTableItemClicked);
	connect(_lineEdit, Signal_Focused, &ExtentionEditor::slotEditWidgetFocused);
	connect(_lineEdit, Signal_EditFinished, &ExtentionEditor::slotLineEditFinished);
	connect(_styleComboBox, Signal_Focused, &ExtentionEditor::slotEditWidgetFocused);
	connect(_styleComboBox, Signal_ItemClicked, &ExtentionEditor::slotStyleComboItemClicked);

	connect(_confirmButton, Signal_Clicked, &ExtentionEditor::slotConfirmButtonClicked);
	connect(_applyButton, Signal_Clicked, &ExtentionEditor::slotApplyButtonClicked);
	connect(_cancelButton, Signal_Clicked, &ExtentionEditor::slotCancelButtonClicked);

}
ExtentionEditor::~ExtentionEditor()
{

}

void ExtentionEditor::slotAssetChanged(BObject* object, const BValue& value)
{
	Settings* settings = value;
	_tableWidget->clearItems();
	const StringTable& exttable = settings->extentionTable();
	for (int r = 0; r < exttable.row(); r++)
	{
		int row = _tableWidget->addRow(r);
		_tableWidget->setItem(row, 0, exttable.at(row, 0));
		_tableWidget->setItem(row, 1, exttable.at(row, 1));
	}
}

void ExtentionEditor::slotInsertButtonClicked(BObject* object, const BValue& value)
{
	int row = _tableWidget->rowCount();
	if (BTableItem* item = _tableWidget->selectedItem())
		row = item->row();
	_tableWidget->insertRow(row);
	_tableWidget->addItem(row, 0);
	_tableWidget->addItem(row, 1);
	_tableWidget->setRowSelected(row, true);
	this->emit(Signal_Changed);
}
void ExtentionEditor::slotRemoveButtonClicked(BObject* object, const BValue& value)
{

}

void ExtentionEditor::slotTableItemClicked(BObject* object, const BValue& value)
{
	BTableItem* item = value;
	if (item)
	{
		if (item->column() == 0)
		{
			if (_lineEdit->asset() != value)
			{
				_lineEdit->setAsset(value);
				_lineEdit->hide();
			}
			else
			{
				_lineEdit->setRect(item->rect());
				_lineEdit->setText(item->text());
				_lineEdit->raise();
				_lineEdit->show();
			}
		}
		if (item->column() == 1)
		{
			if (_styleComboBox->asset() != value)
			{
				_styleComboBox->setAsset(value);
				_styleComboBox->hide();
			}
			else
			{
				_styleComboBox->setRect(item->rect());
				_styleComboBox->setText(item->text());
				_styleComboBox->raise();
				_styleComboBox->show();
			}
		}
	}
	else
	{
		_lineEdit->setAsset(BValue());
		_lineEdit->hide();
		_styleComboBox->setAsset(BValue());
		_styleComboBox->hide();
	}
}
void ExtentionEditor::slotEditWidgetFocused(BObject* object, const BValue& value)
{
	if (value == false)
	{
		_lineEdit->setAsset(BValue());
		_lineEdit->hide();
		_styleComboBox->setAsset(BValue());
		_styleComboBox->hide();
	}
}
void ExtentionEditor::slotLineEditFinished(BObject* object, const BValue& value)
{
	BTableItem* item = _lineEdit->asset();
	item->setText(_lineEdit->text());
	this->emit(Signal_Changed);
}
void ExtentionEditor::slotStyleComboItemClicked(BObject* object, const BValue& value)
{
	BTableItem* item = _styleComboBox->asset();
	item->setText(_styleComboBox->text());
	this->emit(Signal_Changed);
}

void ExtentionEditor::slotConfirmButtonClicked(BObject* object, const BValue& value)
{

}
void ExtentionEditor::slotApplyButtonClicked(BObject* object, const BValue& value)
{
	StringTable extable(_tableWidget->rowCount(), 2);
	for (int r = 0; r < _tableWidget->rowCount(); r++)
	{
		const BString& ext = _tableWidget->item(r, 0)->text();
		const BString& style = _tableWidget->item(r, 1)->text();
		extable.set(r, 0, ext);
		extable.set(r, 1, style);
	}
	if (Settings* settings = this->asset())
	{
		settings->setExtentionTable(extable);
	}
}
void ExtentionEditor::slotCancelButtonClicked(BObject* object, const BValue& value)
{

}
